//
//  File.swift
//
//
//  Created by didi on 2022/9/9.
//

/// generic API model
public protocol ObjectModel: ObjectComponent {
    /// the associated Module
    associatedtype Module: ObjectModule

    /// key to identify a given object (e.g. userId)
    static var pathIdKey: String { get }

    /// full path of the url (module path key + model path key)
    static var urlPath: String { get }

    /// full path of the id url (module path key + model path key + :id path key)
    static var idUrlPath: String { get }

    /// full path of the asset (module asset key + model asset key)
    static var assetPath: String { get }
}

public extension ObjectModel {
    static var pathIdKey: String { String(describing: self).lowercased() + "Id" }
    static var pathKey: String { String(describing: self).lowercased() + "s" }
    static var assetKey: String { pathKey }

    static var urlPath: String { Module.pathKey + "/" + pathKey }
    static var idUrlPath: String { urlPath + "/:" + pathIdKey }
    static var assetPath: String { Module.assetKey + "/" + assetKey }

    static func permission(for action: Permission.Action) -> Permission {
        .init(namespace: Module.permissionKey, context: permissionKey, action: action)
    }
}
